import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

//leetcode 剑指offer041 滑动窗口的平均值
class MovingAverage {
    private Queue<Double> vals = null;
    private int count = 0;
    private double sum = 0;
    private int size = 0;

    /** Initialize your data structure here. */
    public MovingAverage(int size) {
        this.size = size;
        vals = new LinkedList<>();
    }
    
    public double next(int val) {
        if(count < size) {
            vals.offer((double)val);
            sum += val;
            count++;
            return sum / count;
        }
        if(count == size) {
            Double tmp = vals.poll();
            vals.offer((double)val);
            sum -= tmp;
            sum += val;
            return sum / count;
        }
        return 0;
    }
}